{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  face++ Detect API(面部检测)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "api_secret = \"TutG56OCxGDuQjRc_429aQVge1qN9212\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'SsJKglqGQZdoT9kbp7F5td-Gw6JHtn_k'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'http://hong_ling_ya.gitee.io/api-1021/picture/chuzhong.jpg'\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\": img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603455915,2efc1049-fd8d-4f85-9219-46438a71e4f4\",\"time_used\":4422,\"faces\":[{\"face_token\":\"03a07ef004dcafc7053127f967fcbb45\",\"face_rectangle\":{\"top\":154,\"left\":121,\"width\":272,\"height\":272},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":9},\"smile\":{\"value\":100.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.007,\"disgust\":0.007,\"fear\":0.007,\"happiness\":97.750,\"neutral\":0.020,\"sadness\":2.203,\"surprise\":0.007}}}],\"image_id\":\"S3vWfDYn8fLlpmOmDddbWg==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603456773,e18e6017-858e-4610-bef2-1fefc378d105',\n",
       " 'time_used': 647,\n",
       " 'faces': [{'face_token': 'e6336cd9ecadab83cbd9621e390fcb3b',\n",
       "   'face_rectangle': {'top': 165, 'left': 984, 'width': 360, 'height': 360},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 19},\n",
       "    'smile': {'value': 16.608, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 3.832,\n",
       "     'disgust': 0.034,\n",
       "     'fear': 0.076,\n",
       "     'happiness': 0.05,\n",
       "     'neutral': 93.754,\n",
       "     'sadness': 2.219,\n",
       "     'surprise': 0.034}}},\n",
       "  {'face_token': 'af1f0967c3032ba8c97441a8a2d869d1',\n",
       "   'face_rectangle': {'top': 545, 'left': 379, 'width': 180, 'height': 180},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'value': 5.34, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.015,\n",
       "     'disgust': 0.015,\n",
       "     'fear': 0.016,\n",
       "     'happiness': 1.716,\n",
       "     'neutral': 81.596,\n",
       "     'sadness': 0.015,\n",
       "     'surprise': 16.627}}},\n",
       "  {'face_token': 'fb399ca46b8a8200872cf0008c309aa1',\n",
       "   'face_rectangle': {'top': 166, 'left': 740, 'width': 103, 'height': 103},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 28},\n",
       "    'smile': {'value': 90.769, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.592,\n",
       "     'disgust': 0.35,\n",
       "     'fear': 0.35,\n",
       "     'happiness': 52.198,\n",
       "     'neutral': 36.433,\n",
       "     'sadness': 0.35,\n",
       "     'surprise': 9.726}}},\n",
       "  {'face_token': '0b8d3d7eb85bf7be1b747f3884a0679a',\n",
       "   'face_rectangle': {'top': 360, 'left': 689, 'width': 94, 'height': 94},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 26},\n",
       "    'smile': {'value': 99.998, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 100.0,\n",
       "     'neutral': 0.0,\n",
       "     'sadness': 0.0,\n",
       "     'surprise': 0.0}}}],\n",
       " 'image_id': 'k7spLtP7CViJ6yStDU30Qw==',\n",
       " 'face_num': 4}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# requests 巧妙的方法   r = response\n",
    "results = r.json() # \n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "# from pandas.io.json import json_normalize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# results['faces']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>attributes</th>\n",
       "      <th>face_rectangle</th>\n",
       "      <th>face_token</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'gender': {'value': 'Female'}, 'age': {'value...</td>\n",
       "      <td>{'top': 154, 'left': 121, 'width': 272, 'heigh...</td>\n",
       "      <td>03a07ef004dcafc7053127f967fcbb45</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          attributes  \\\n",
       "0  {'gender': {'value': 'Female'}, 'age': {'value...   \n",
       "\n",
       "                                      face_rectangle  \\\n",
       "0  {'top': 154, 'left': 121, 'width': 272, 'heigh...   \n",
       "\n",
       "                         face_token  \n",
       "0  03a07ef004dcafc7053127f967fcbb45  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>attributes</th>\n",
       "      <th>face_rectangle</th>\n",
       "      <th>face_token</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'gender': {'value': 'Male'}, 'age': {'value':...</td>\n",
       "      <td>{'top': 165, 'left': 984, 'width': 360, 'heigh...</td>\n",
       "      <td>e6336cd9ecadab83cbd9621e390fcb3b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'gender': {'value': 'Male'}, 'age': {'value':...</td>\n",
       "      <td>{'top': 545, 'left': 379, 'width': 180, 'heigh...</td>\n",
       "      <td>af1f0967c3032ba8c97441a8a2d869d1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'gender': {'value': 'Female'}, 'age': {'value...</td>\n",
       "      <td>{'top': 166, 'left': 740, 'width': 103, 'heigh...</td>\n",
       "      <td>fb399ca46b8a8200872cf0008c309aa1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'gender': {'value': 'Female'}, 'age': {'value...</td>\n",
       "      <td>{'top': 360, 'left': 689, 'width': 94, 'height...</td>\n",
       "      <td>0b8d3d7eb85bf7be1b747f3884a0679a</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          attributes  \\\n",
       "0  {'gender': {'value': 'Male'}, 'age': {'value':...   \n",
       "1  {'gender': {'value': 'Male'}, 'age': {'value':...   \n",
       "2  {'gender': {'value': 'Female'}, 'age': {'value...   \n",
       "3  {'gender': {'value': 'Female'}, 'age': {'value...   \n",
       "\n",
       "                                      face_rectangle  \\\n",
       "0  {'top': 165, 'left': 984, 'width': 360, 'heigh...   \n",
       "1  {'top': 545, 'left': 379, 'width': 180, 'heigh...   \n",
       "2  {'top': 166, 'left': 740, 'width': 103, 'heigh...   \n",
       "3  {'top': 360, 'left': 689, 'width': 94, 'height...   \n",
       "\n",
       "                         face_token  \n",
       "0  e6336cd9ecadab83cbd9621e390fcb3b  \n",
       "1  af1f0967c3032ba8c97441a8a2d869d1  \n",
       "2  fb399ca46b8a8200872cf0008c309aa1  \n",
       "3  0b8d3d7eb85bf7be1b747f3884a0679a  "
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 立马试试pd.DataFrame 並观察\n",
    "faces_data = results['faces']\n",
    "df = pd.DataFrame(faces_data)\n",
    "df\n",
    "\n",
    "# attributes, face_rectangle, face_token\n",
    "# 查一下API文檔"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    {'top': 165, 'left': 984, 'width': 360, 'heigh...\n",
       "1    {'top': 545, 'left': 379, 'width': 180, 'heigh...\n",
       "2    {'top': 166, 'left': 740, 'width': 103, 'heigh...\n",
       "3    {'top': 360, 'left': 689, 'width': 94, 'height...\n",
       "Name: face_rectangle, dtype: object"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取变量 (pandas cheetsheet)\n",
    "df[\"face_rectangle\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: {'top': 165, 'left': 984, 'width': 360, 'height': 360},\n",
       " 1: {'top': 545, 'left': 379, 'width': 180, 'height': 180},\n",
       " 2: {'top': 166, 'left': 740, 'width': 103, 'height': 103},\n",
       " 3: {'top': 360, 'left': 689, 'width': 94, 'height': 94}}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"face_rectangle\"].to_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>height</th>\n",
       "      <td>360</td>\n",
       "      <td>180</td>\n",
       "      <td>103</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>left</th>\n",
       "      <td>984</td>\n",
       "      <td>379</td>\n",
       "      <td>740</td>\n",
       "      <td>689</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>165</td>\n",
       "      <td>545</td>\n",
       "      <td>166</td>\n",
       "      <td>360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>width</th>\n",
       "      <td>360</td>\n",
       "      <td>180</td>\n",
       "      <td>103</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0    1    2    3\n",
       "height  360  180  103   94\n",
       "left    984  379  740  689\n",
       "top     165  545  166  360\n",
       "width   360  180  103   94"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(df[\"face_rectangle\"].to_dict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>left</th>\n",
       "      <th>top</th>\n",
       "      <th>width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>360</td>\n",
       "      <td>984</td>\n",
       "      <td>165</td>\n",
       "      <td>360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>180</td>\n",
       "      <td>379</td>\n",
       "      <td>545</td>\n",
       "      <td>180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>103</td>\n",
       "      <td>740</td>\n",
       "      <td>166</td>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>94</td>\n",
       "      <td>689</td>\n",
       "      <td>360</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  left  top  width\n",
       "0     360   984  165    360\n",
       "1     180   379  545    180\n",
       "2     103   740  166    103\n",
       "3      94   689  360     94"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(df[\"face_rectangle\"].to_dict()).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_rectangle_height</th>\n",
       "      <th>face_rectangle_left</th>\n",
       "      <th>face_rectangle_top</th>\n",
       "      <th>face_rectangle_width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>360</td>\n",
       "      <td>984</td>\n",
       "      <td>165</td>\n",
       "      <td>360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>180</td>\n",
       "      <td>379</td>\n",
       "      <td>545</td>\n",
       "      <td>180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>103</td>\n",
       "      <td>740</td>\n",
       "      <td>166</td>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>94</td>\n",
       "      <td>689</td>\n",
       "      <td>360</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   face_rectangle_height  face_rectangle_left  face_rectangle_top  \\\n",
       "0                    360                  984                 165   \n",
       "1                    180                  379                 545   \n",
       "2                    103                  740                 166   \n",
       "3                     94                  689                 360   \n",
       "\n",
       "   face_rectangle_width  \n",
       "0                   360  \n",
       "1                   180  \n",
       "2                   103  \n",
       "3                    94  "
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_rect = pd.DataFrame(df[\"face_rectangle\"].to_dict()).T\n",
    "# 欄位名稱加上 face_rectangle \n",
    "df_rect.columns = [ \"face_rectangle_\"+x for x in df_rect.columns]\n",
    "df_rect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>attributes</th>\n",
       "      <th>face_rectangle</th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle_height</th>\n",
       "      <th>face_rectangle_left</th>\n",
       "      <th>face_rectangle_top</th>\n",
       "      <th>face_rectangle_width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'gender': {'value': 'Male'}, 'age': {'value':...</td>\n",
       "      <td>{'top': 165, 'left': 984, 'width': 360, 'heigh...</td>\n",
       "      <td>e6336cd9ecadab83cbd9621e390fcb3b</td>\n",
       "      <td>360</td>\n",
       "      <td>984</td>\n",
       "      <td>165</td>\n",
       "      <td>360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'gender': {'value': 'Male'}, 'age': {'value':...</td>\n",
       "      <td>{'top': 545, 'left': 379, 'width': 180, 'heigh...</td>\n",
       "      <td>af1f0967c3032ba8c97441a8a2d869d1</td>\n",
       "      <td>180</td>\n",
       "      <td>379</td>\n",
       "      <td>545</td>\n",
       "      <td>180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'gender': {'value': 'Female'}, 'age': {'value...</td>\n",
       "      <td>{'top': 166, 'left': 740, 'width': 103, 'heigh...</td>\n",
       "      <td>fb399ca46b8a8200872cf0008c309aa1</td>\n",
       "      <td>103</td>\n",
       "      <td>740</td>\n",
       "      <td>166</td>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'gender': {'value': 'Female'}, 'age': {'value...</td>\n",
       "      <td>{'top': 360, 'left': 689, 'width': 94, 'height...</td>\n",
       "      <td>0b8d3d7eb85bf7be1b747f3884a0679a</td>\n",
       "      <td>94</td>\n",
       "      <td>689</td>\n",
       "      <td>360</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          attributes  \\\n",
       "0  {'gender': {'value': 'Male'}, 'age': {'value':...   \n",
       "1  {'gender': {'value': 'Male'}, 'age': {'value':...   \n",
       "2  {'gender': {'value': 'Female'}, 'age': {'value...   \n",
       "3  {'gender': {'value': 'Female'}, 'age': {'value...   \n",
       "\n",
       "                                      face_rectangle  \\\n",
       "0  {'top': 165, 'left': 984, 'width': 360, 'heigh...   \n",
       "1  {'top': 545, 'left': 379, 'width': 180, 'heigh...   \n",
       "2  {'top': 166, 'left': 740, 'width': 103, 'heigh...   \n",
       "3  {'top': 360, 'left': 689, 'width': 94, 'height...   \n",
       "\n",
       "                         face_token  face_rectangle_height  \\\n",
       "0  e6336cd9ecadab83cbd9621e390fcb3b                    360   \n",
       "1  af1f0967c3032ba8c97441a8a2d869d1                    180   \n",
       "2  fb399ca46b8a8200872cf0008c309aa1                    103   \n",
       "3  0b8d3d7eb85bf7be1b747f3884a0679a                     94   \n",
       "\n",
       "   face_rectangle_left  face_rectangle_top  face_rectangle_width  \n",
       "0                  984                 165                   360  \n",
       "1                  379                 545                   180  \n",
       "2                  740                 166                   103  \n",
       "3                  689                 360                    94  "
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用.join合併\n",
    "df.join(df_rect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    {'gender': {'value': 'Male'}, 'age': {'value':...\n",
       "1    {'gender': {'value': 'Male'}, 'age': {'value':...\n",
       "2    {'gender': {'value': 'Female'}, 'age': {'value...\n",
       "3    {'gender': {'value': 'Female'}, 'age': {'value...\n",
       "Name: attributes, dtype: object"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"attributes\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 {'anger': 3.832, 'disgust': 0.034, 'fear': 0.076, 'happiness': 0.05, 'neutral': 93.754, 'sadness': 2.219, 'surprise': 0.034}\n",
      "1 {'anger': 0.015, 'disgust': 0.015, 'fear': 0.016, 'happiness': 1.716, 'neutral': 81.596, 'sadness': 0.015, 'surprise': 16.627}\n",
      "2 {'anger': 0.592, 'disgust': 0.35, 'fear': 0.35, 'happiness': 52.198, 'neutral': 36.433, 'sadness': 0.35, 'surprise': 9.726}\n",
      "3 {'anger': 0.0, 'disgust': 0.0, 'fear': 0.0, 'happiness': 100.0, 'neutral': 0.0, 'sadness': 0.0, 'surprise': 0.0}\n"
     ]
    }
   ],
   "source": [
    "for i in df[\"attributes\"].index:\n",
    "    # print (i,df[\"attributes\"].loc[i][\"emotion\"])\n",
    "    print (i,df[\"attributes\"].loc[i].get(\"emotion\")  )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'anger': 3.832,\n",
       "  'disgust': 0.034,\n",
       "  'fear': 0.076,\n",
       "  'happiness': 0.05,\n",
       "  'neutral': 93.754,\n",
       "  'sadness': 2.219,\n",
       "  'surprise': 0.034},\n",
       " {'anger': 0.015,\n",
       "  'disgust': 0.015,\n",
       "  'fear': 0.016,\n",
       "  'happiness': 1.716,\n",
       "  'neutral': 81.596,\n",
       "  'sadness': 0.015,\n",
       "  'surprise': 16.627},\n",
       " {'anger': 0.592,\n",
       "  'disgust': 0.35,\n",
       "  'fear': 0.35,\n",
       "  'happiness': 52.198,\n",
       "  'neutral': 36.433,\n",
       "  'sadness': 0.35,\n",
       "  'surprise': 9.726},\n",
       " {'anger': 0.0,\n",
       "  'disgust': 0.0,\n",
       "  'fear': 0.0,\n",
       "  'happiness': 100.0,\n",
       "  'neutral': 0.0,\n",
       "  'sadness': 0.0,\n",
       "  'surprise': 0.0}]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[df[\"attributes\"].loc[i][\"emotion\"] if type(df[\"attributes\"].loc[i])  is dict else missing_value for i in df[\"attributes\"].index ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>attributes_emotion_anger</th>\n",
       "      <th>attributes_emotion_disgust</th>\n",
       "      <th>attributes_emotion_fear</th>\n",
       "      <th>attributes_emotion_happiness</th>\n",
       "      <th>attributes_emotion_neutral</th>\n",
       "      <th>attributes_emotion_sadness</th>\n",
       "      <th>attributes_emotion_surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3.832</td>\n",
       "      <td>0.034</td>\n",
       "      <td>0.076</td>\n",
       "      <td>0.050</td>\n",
       "      <td>93.754</td>\n",
       "      <td>2.219</td>\n",
       "      <td>0.034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.015</td>\n",
       "      <td>0.015</td>\n",
       "      <td>0.016</td>\n",
       "      <td>1.716</td>\n",
       "      <td>81.596</td>\n",
       "      <td>0.015</td>\n",
       "      <td>16.627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.592</td>\n",
       "      <td>0.350</td>\n",
       "      <td>0.350</td>\n",
       "      <td>52.198</td>\n",
       "      <td>36.433</td>\n",
       "      <td>0.350</td>\n",
       "      <td>9.726</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>100.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   attributes_emotion_anger  attributes_emotion_disgust  \\\n",
       "0                     3.832                       0.034   \n",
       "1                     0.015                       0.015   \n",
       "2                     0.592                       0.350   \n",
       "3                     0.000                       0.000   \n",
       "\n",
       "   attributes_emotion_fear  attributes_emotion_happiness  \\\n",
       "0                    0.076                         0.050   \n",
       "1                    0.016                         1.716   \n",
       "2                    0.350                        52.198   \n",
       "3                    0.000                       100.000   \n",
       "\n",
       "   attributes_emotion_neutral  attributes_emotion_sadness  \\\n",
       "0                      93.754                       2.219   \n",
       "1                      81.596                       0.015   \n",
       "2                      36.433                       0.350   \n",
       "3                       0.000                       0.000   \n",
       "\n",
       "   attributes_emotion_surprise  \n",
       "0                        0.034  \n",
       "1                       16.627  \n",
       "2                        9.726  \n",
       "3                        0.000  "
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_attr_emotion = pd.DataFrame([df[\"attributes\"].loc[i][\"emotion\"] if type(df[\"attributes\"].loc[i])  is dict else missing_value['emotion'] for i in df[\"attributes\"].index ])\n",
    "# 欄位名稱加上 face_rectangle \n",
    "df_attr_emotion.columns = [ \"attributes_emotion_\"+x for x in df_attr_emotion.columns]\n",
    "df_attr_emotion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
